/*
 * WeakReplicaManagerModule.java    0.0.1    09/dic/2009
 * 
 * Copyright (c) 2009 mentalsmash.org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * The use of the Apache License does not indicate that this project is
 * affiliated with the Apache Software Foundation.
 */
package org.mentalsmash.tazio.transparency.configuration;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.mentalsmash.tazio.commons.identifiers.NID;
import org.mentalsmash.tazio.transparency.impl.BaseInputDispatcher;
import org.mentalsmash.tazio.transparency.impl.BaseMessageFactory;
import org.mentalsmash.tazio.transparency.impl.BaseMessageFilter;
import org.mentalsmash.tazio.transparency.impl.TransparencyMonitorImpl;
import org.mentalsmash.tazio.transparency.impl.TransparencyImpl;
import org.mentalsmash.tazio.transparency.impl.WeakReplicaManagerResolver;
import org.mentalsmash.tazio.transparency.interfaces.Dispatcher;
import org.mentalsmash.tazio.transparency.interfaces.MessageFactory;
import org.mentalsmash.tazio.transparency.interfaces.MessageFilter;
import org.mentalsmash.tazio.transparency.interfaces.Resolver;
import org.mentalsmash.tazio.transparency.interfaces.TransparencyMonitor;
import org.mentalsmash.tazio.transparency.interfaces.Transparency;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.AbstractModule;
import com.google.inject.name.Names;

/**
 * This extension to {@link AbstractModule} is used to create the bindings for
 * the WeakReplicaManager's side of Tazio's transparency layer subsystem.
 * 
 * @version 0.0.1 09/dic/2009
 * @author Enrico Savioli <savioli.enrico@gmail.com>
 */
public class WeakReplicaManagerModule extends AbstractModule {

    private final static Logger log = LoggerFactory
	    .getLogger(WeakReplicaManagerModule.class);

    private final String propertyFile;

    public WeakReplicaManagerModule(String propertyFile) {
	this.propertyFile = propertyFile;
    }
    
    
    /*
     * (non-Javadoc)
     * 
     * @see com.google.inject.AbstractModule#configure()
     */
    @Override
    protected void configure() {

	try {
	    Properties props = loadProperties();
	    Names.bindProperties(binder(), props);
	} catch (IOException e) {
	    String msg = "Input/Output error while loading the configuaration file";
	    log.error(msg);
	    e.printStackTrace();
	}

	bind(NID.class).annotatedWith(Names.named("node_id")).toInstance(
		NID.generateNID());

	bind(Dispatcher.class).to(BaseInputDispatcher.class);
	bind(MessageFactory.class).to(BaseMessageFactory.class);
	bind(MessageFilter.class).to(BaseMessageFilter.class).asEagerSingleton();
	bind(Resolver.class).to(WeakReplicaManagerResolver.class);
	bind(Transparency.class).to(TransparencyImpl.class);

	// test
	bind(TransparencyMonitor.class).to(TransparencyMonitorImpl.class);
    }

    private Properties loadProperties() throws IOException {

	Properties props = new Properties();
	FileInputStream inStream = new FileInputStream(propertyFile);
	props.load(inStream);
	inStream.close();

	return props;

    }

}
